<template>
  <div class="container">
    <el-tree :data="depts" :props="defaultProps" :default-expand-all="true" :expand-on-click-node="false">
      <template #default="{data}">
        <!-- el-row 行 el-col 列  -->
        <el-row style="width:100%" type="flex" justify="space-between" align="middle">
          <el-col>{{ data.name }}</el-col>
          <el-col :span="4">
            <span class="manager-name">{{ data.managerName }}</span>
            <!--
              openDepts($event) === openDepts
              -->
            <el-dropdown @command="openDepts($event,data.id)">
              <span class="el-dropdown-link">
                操作
                <i class="el-icon-arrow-down el-icon--right" />
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                <el-dropdown-item command="del">删除</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </el-col>
        </el-row>
      </template>
    </el-tree>
    <!-- 封装添加部门的组件 -->
    <!-- <AddDeptVue
      :show-dialog="showDialog"
      @update:showDialog="showDialog=$event"
    /> -->
    <AddDeptVue
      ref="addDept"
      :show-dialog.sync="showDialog"
      :depart-id="departId"
      @updateDepartmentList="getDeps"
    />
  </div>
</template>
<script>
import { delDepartment, getDepartmentList } from '@/api/department'
import { translateListToTreeData } from '@/utils'
import AddDeptVue from './components/AddDept.vue'

export default {
  name: 'Department',
  components: {
    AddDeptVue
  },
  data() {
    return {
      // 下拉菜单触发点击事件时所对应部门的id
      departId: null,
      showDialog: false,
      depts: [
        // {
        //   name: '传智教育',
        //   managerName: '室友鹏',
        //   children: [
        //   { name: '总裁办',
        //     managerName: '周围'
        //   },
        //   { name: '行政部',
        //     managerName: '皮尔'
        //   },
        //   { name: '人事部',
        //     managerName: '何亮',

        //     children: [
        //       { name: '财务核算部', managerName: '牛力' },
        //       { name: '税务核算部',
        //         managerName: '阿珂哥'
        //       },
        //       { name: '薪资核算部',
        //         managerName: '绝绝'
        //       }
        //     ]
        //   }
        // ]}
      ],
      defaultProps: {
        label: 'name', // 标签的属性
        children: 'children' // 子元素的属性
      }
    }
  },
  created() {
    this.getDeps()
  },
  methods: {
    async  openDepts(type, id) {
      this.departId = id
      if (type === 'add') {
        // 显示弹层 - 控制弹层显示隐藏变量 / true
        this.showDialog = true
      } else if (type === 'edit') {
        // 显示弹层 - 控制弹层显示隐藏变量 / true
        this.showDialog = true
        //  调用子组件的获取部门详情的方法
        // 1. 获取子组件的实例 ref 和 $refs
        // 2. 调用实例身上的方法
        // vue更新dom的过程是异步 - 提高性能
        // 一旦数据发生了变化,组件实例和dom更新渲染是异步的
        // $nextTick 获取到更新后的dom和组件实例
        this.$nextTick(() => {
          this.$refs.addDept.getDepartmentDetails()
        })
      } else {
        // 删除
        // 1. 弹出确认框
        await this.$confirm('确认要删除此部门么?', '温馨提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消'
        })
        // 2. 调用删除的API
        await delDepartment(id)
        // 3. 提示用户
        this.$message.success('删除部门成功')
        // 4. 重新获取部门列表
        this.getDeps()
      }
    },
    async getDeps() {
      const res = await getDepartmentList()
      console.log(res)
      this.depts = translateListToTreeData(res, 0)
    }
  }
}
</script>
<style scoped>
.container{
  padding: 30px 150px;
  font-size: 14px;
}
.manager-name{
  width: 150px;
  margin-right: 10px;
}
</style>
